home *** CD-ROM | disk | FTP | other *** search
- /*
- HASUtilTest.c from Hsoi's App Shell ©1995-1997 John C. Daub. All rights reserved.
-
- This file contains various utility functions for use with the HAS Test code to
- help you display various things in the message window.
- */
-
- #pragma mark ••• #includes •••
-
- #ifndef __HSOIS_APP_SHELL__
- #include "HASMain.h"
- #endif
- #include "HASGlobals.h"
- #include "HASMenus.h"
- #include "HASMenuWindows.h"
- #include "HASSoundSpeech.h"
- #include "HASTest.h"
- #include "HASUtilTest.h"
-
- #pragma mark -
- #pragma mark ••• Test Window Creation/Destruction •••
-
- // this loads up and prepares our test window
-
- WindowRef HsoiGetTestWindow( void )
- {
-
- // get our test window if we don't already have it
-
- if ( gTestWindow == nil )
- gTestWindow = GetNewWindow( rTestWIND, nil, MOVE_TO_FRONT );
-
- // some crude error handling
-
- if ( gTestWindow == nil )
- {
- SysBeep( 5 );
- return nil;
- }
-
- // set the port
-
- SetPortWindowPort( gTestWindow );
-
- // set the windowKind for identification
-
- SetWindowKind( gTestWindow, kTestWindowKind );
-
- // make the window visible
-
- ShowWindow( gTestWindow );
-
- // bring it to the front (if it's in the background)
-
- SelectWindow( gTestWindow );
-
- // adjust the menus to suit
-
- HsoiAdjustMenus();
-
- // and return the window
-
- return gTestWindow;
- }
-
- // this will remove the test window from existance on this earth
-
- void HsoiDestroyTestWindow( void )
- {
- // simple enough...just dispose it (nice there's nothing attached to the window )
-
- DisposeWindow( gTestWindow );
-
- // set the WindowRef to nil for better error checking (using nil variables
- // usually turns up bad consequences)
-
- gTestWindow = nil;
-
- // and adjust the menus to fit
-
- HsoiAdjustMenus();
-
- return;
- }
-
- #pragma mark -
- #pragma mark ••• Test Window Utils •••
-
- // checks the window passed to it to see if it's the test window
-
- Boolean HsoiIsTestWindow( WindowRef window )
- {
- if ( window == nil )
- return false;
-
- else
- return ( GetWindowKind(window) == kTestWindowKind );
- }
-
-
- // our routine to adjust the menus when the test window is frontmost
-
- void HsoiAdjustMenusTestWindow( WindowRef window )
- {
- MenuRef menu;
- short i;
-
- // Apple Menu -- enable it all
-
- menu = GetMenuHandle( mApple );
-
- for ( i = 0; i <= CountMenuItems( menu ); i++ )
- EnableItem( menu, i );
-
- // File Menu
-
- menu = GetMenuHandle( mFile );
- for ( i = 0; i <= CountMenuItems( menu ); i++ )
- EnableItem( menu, i );
-
- // if we have hit the max number of open document windows, we must disable New
- // and Open
-
- if ( gNumWindows >= kMaxNumberOfOpenWindows )
- {
- DisableItem( menu, iNew );
- DisableItem( menu, iOpen );
- }
-
- DisableItem( menu, iSave );
- DisableItem( menu, iSaveAs );
- DisableItem( menu, iRevert );
- DisableItem( menu, iPrint );
-
- // Edit Menu -- not much to do here either...
-
- menu = GetMenuHandle( mEdit );
- for ( i = 0; i <= CountMenuItems( menu ); i++ )
- EnableItem( menu, i );
-
- DisableItem( menu, iUndo );
- DisableItem( menu, iCut );
- DisableItem( menu, iCopy );
- DisableItem( menu, iPaste );
- DisableItem( menu, iClear );
- DisableItem( menu, iSelectAll );
-
- // Text Menu (and it's submenus...cause this is pretty straight forward)
-
- DisableItem( GetMenuHandle( mText ), 0 );
- DisableItem( GetMenuHandle( mFont ), 0 );
- DisableItem( GetMenuHandle( mSize ), 0 );
- DisableItem( GetMenuHandle( mStyle ), 0 );
- DisableItem( GetMenuHandle( mJustification ), 0 );
- DisableItem( GetMenuHandle( mColor ), 0 );
- DisableItem( GetMenuHandle( mFeatures ), 0 );
-
- // Dialogs Menu
-
- menu = GetMenuHandle( mDialogs );
- for ( i = 0; i <= CountMenuItems( menu ); i++ )
- EnableItem( menu, i );
-
- // Windows menu
-
- menu = GetMenuHandle( mWindows );
- for ( i = 0; i <= CountMenuItems( menu ); i++ )
- EnableItem( menu, i );
- HsoiAdjustWindowsMenu( window );
-
- // Sound menu (and Voices submenu)
-
- HsoiAdjustSoundMenu( window );
-
- // Help menu
-
- HMGetHelpMenuHandle( &menu );
- if ( gHiliting )
- DisableItem( menu, 0 );
- else
- {
- for ( i = 0; i <= CountMenuItems( menu ); i++ )
- EnableItem( menu, i );
- }
-
- // Test menu
-
- menu = GetMenuHandle( mTestMENU );
- if ( gHiliting )
- DisableItem( menu, 0 );
- else
- {
- for ( i = 0; i <= CountMenuItems( menu ); i++ )
- EnableItem( menu, i );
- }
-
-
-
- return;
- }
-
- #pragma mark -
- #pragma mark ••• Misc Testing Utils •••
-
- // what follows are various utility routines nice for helping you do things
- // with the test code and window. note how many are hard coded to use the test window.
- // if you're going to use any of these functions, you need to make sure that you've
- // already gotten the gTestWindow, possibly stopped any currently playing sounds, etc.
- // see HsoiDoTest() for a sample.
-
- void HsoiShowError( Str255 errorMessage, long errorNumber )
- {
- WindowRef window;
- Str255 numberString;
-
- NumToString( errorNumber, numberString );
-
- GetWindowPort( &window );
-
- SetPortWindowPort( gTestWindow );
- EraseRect( &GetWindowPort(gTestWindow)->portRect );
- MoveTo( 10, 20 );
-
- DrawString( errorMessage );
- DrawString( numberString );
-
- SetPortWindowPort( window );
-
- return;
- }
-
-
- // PrintOSType: display an OSType string in the test window
- void HsoiPrintOSType( OSType theType )
- {
- Str255 typeString;
- WindowRef window;
- short i;
-
- typeString[0] = 4;
-
- for ( i = 0; i <= 3; i++ )
- {
- typeString[ 4 - i ] = (char)(theType & 0x000000FF );
- theType = theType >> 8;
- }
-
- GetWindowPort( &window );
-
- SetPortWindowPort( gTestWindow );
- EraseRect( &GetWindowPort(gTestWindow)->portRect );
- MoveTo( 10, 20 );
- DrawString( typeString );
-
- SetPortWindowPort( window );
-
- return;
- }
-
-
- // PrintString: display a string in the test Window
-
- void HsoiPrintString( Str255 s )
- {
- WindowRef window;
-
- if ( !s )
- return;
-
- GetWindowPort( &window );
-
- SetPortWindowPort( gTestWindow );
- EraseRect( &GetWindowPort(gTestWindow)->portRect );
- MoveTo( 10, 20 );
-
- DrawString( s );
-
- SetPortWindowPort( window );
-
- return;
- }
-
-
- // PrintHex: convert number to a hex string and display in test Window
-
- void HsoiPrintHex( long theNumber )
- {
- char theString[10];
- long digit;
- short i;
- WindowRef window;
-
- GetWindowPort( &window );
-
- SetPortWindowPort( gTestWindow );
- EraseRect( &GetWindowPort(gTestWindow)->portRect );
- MoveTo( 10, 20 );
-
- theString[ 0 ] = 9;
- theString[ 1 ] = '$';
-
- for ( i = 0; i <= 7; i++ )
- {
- digit = theNumber & 0x0000000F;
-
- if ( digit < 10 )
- {
- digit += (long)('0');
- }
- else
- {
- digit += (long)( 'A' - 10 );
- }
-
- theString[ 9 - i ] = (char)digit;
- theNumber = theNumber >> 4;
- }
-
- DrawString( (StringPtr)theString );
-
- SetPortWindowPort( window );
-
- return;
- }
-
-
- // CRLF: advance drawing position in front window to left side of next line
-
- void HsoiCRLF( void )
- {
-
- Point currentPosition;
- FontInfo theFontInfo;
- register short lineHeight;
-
- GetPen( ¤tPosition );
-
- GetFontInfo( &theFontInfo );
- lineHeight = theFontInfo.ascent + theFontInfo.descent + theFontInfo.leading;
-
- MoveTo( kLeftMargin, currentPosition.v + lineHeight );
-
- return;
- }
-